package com.my.crawler;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.mysql.utils.WeiBoContentDBUtils;
import com.plf.model.WeiBoMain;

import cn.edu.hfut.dmic.webcollector.crawler.DeepCrawler;
import cn.edu.hfut.dmic.webcollector.model.Links;
import cn.edu.hfut.dmic.webcollector.model.Page;
import cn.edu.hfut.dmic.webcollector.net.HttpRequesterImpl;

public class WeiboCrawler extends DeepCrawler {

	private String tablename, cookie;
	public static List<String> comments = new ArrayList<String>();
	WeiBoMain wbm;

	public WeiboCrawler(String crawlPath, String cookie, String tablename) {
		super(crawlPath);
		this.cookie = cookie;
		this.tablename = tablename;

		HttpRequesterImpl mhri = (HttpRequesterImpl) this.getHttpRequester();
		mhri.setCookie(cookie);

		WeiBoContentDBUtils.createWeiBoContentTable(tablename);

	}

	@Override
	public Links visitAndGetNextLinks(Page page) {
		Elements weibos = page.getDoc().select("div.c");
		System.out.println("wweibos.size===>" + weibos.size());
		int i = 0;
		for (Element weibo : weibos) {
			i++;
			String wbId = null;
			try {
				wbId = weibo.getElementsByClass("c").attr("id").substring(2);
			} catch (Exception e) {
				e.printStackTrace();
				System.out.println("检测到不合法的divs，直接跳过本次执行");
				System.out.println("cont parse wbid===>"
						+ weibo.getElementsByClass("c").attr("id"));
				continue;
			}

			String relayFromUser = "";
			String wbContent = "";

			String wbtime = "";
			String wbNewCommentUrl = "";
			String wbOldCommentUrl = "";
			String wbRelayReason = "";

			Elements divs = weibo.getElementsByTag("div");
			System.out.println("divs---num---->" + divs.size() + "i==>" + i);
			if (divs.size() == 3 || divs.size() == 2) {
				// 原创微博
				wbContent = weibo.select("span.ctt").text();
				wbNewCommentUrl = weibo.select("a.cc").attr("href");
				wbtime = MyFuntions.WeiBoHandlerTime(weibo.select("span.ct")
						.text());
			} else if (divs.size() == 4) {
				// 转发微博

				relayFromUser = weibo.select("span.cmt").get(0)
						.getElementsByTag("a").text();
				wbContent = weibo.select("span.ctt").text();

				Elements urls = weibo.select("a.cc");
				wbOldCommentUrl = urls.get(0).attr("href");
				wbNewCommentUrl = urls.get(1).attr("href");

				wbtime = MyFuntions.WeiBoHandlerTime(weibo.select("span.ct")
						.text());
				wbRelayReason = "";

			} else {
				System.out.println("div0" + divs.get(0).html() + "/n/ndiv1"
						+ divs.get(1).html());
			}

			System.out.println("test content crawler====>" + wbId + "\n,"
					+ relayFromUser + "\n," + wbContent + "\n,"
					+ wbOldCommentUrl + "\n," + wbRelayReason + "\n,"
					+ wbNewCommentUrl + "\n," + wbtime);
			wbm = new WeiBoMain();
			wbm.setId(wbId);
			wbm.setText(wbContent);
			wbm.setTime(wbtime);
			wbm.setNewCommentUrl(wbNewCommentUrl);
			wbm.setOldCommentUrl(wbOldCommentUrl);
			wbm.setRelayFromUser(relayFromUser);
			wbm.setRelayReason(wbRelayReason);

			WeiBoContentDBUtils.insertWeiBoContentTable(tablename, wbm);

			wbm = null;

		}
		try {
			Thread.sleep(1 * 1000);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}

}
